package com.hezuti.common.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
/**
 * 
 * @author xiaoyu.dou
 *
 */
public class RoutingSynchronizeManager {
	private static final ThreadLocal<String> resource = new ThreadLocal<String>();

	private static Log log = LogFactory.getLog(RoutingSynchronizeManager.class);
	/**
	 * 
	 * @return
	 */
	public static boolean isActive() {
		return resource.get() != null;
	}
	/**
	 * 
	 * @param key
	 */
	public static void bindResource(String key) {
		Assert.notNull(key, "key cannot be null");
		if (log.isDebugEnabled()) {
			log.debug("setting routingSessionFactory key to [" + key + "]");
		}
		resource.set(key);
	}
	/**
	 * 
	 */
	public static void unbindResource() {
		if (isActive()) {
			if (log.isDebugEnabled()) {
				log.debug("routingSessionFactory key [" + resource.get() + "] was removed.");
			}
			resource.remove();
		}
	}
	/**
	 * 
	 * @return
	 */
	public static String getResource() {
		String key = (String) resource.get();
		if (log.isDebugEnabled()) {
			log.debug("the key is [" + key + "]");
		}
		if (key == null) {
			log.warn("Did you *FORGET* to set the routingSessionFactory key?");
		}
		return key;
	}
}
